﻿using AutoMapper;
using HelenServer.UserCenter.Contracts;

namespace HelenServer.UserCenter.Dal;

[ServiceProvider(typeof(IDalPositionService), SqlServerConstants.ProviderName)]
public class DalPositionService : IDalPositionService
{
    private readonly IRepo<UserCenterDbContext> _repo;
    private readonly IMapper _mapper;

    public DalPositionService(UserCenterDbContext context, IMapper mapper)
    {
        _repo = new Repo<UserCenterDbContext>(context);
        _mapper = mapper;
    }

    public async Task<OperationResult<int>> AddAsync(Operation<PositionCreateModel> operation, CancellationToken cancellationToken = default)
    {
        var entity = _mapper.Map<PositionCreateModel, Position>(operation.Parameter);

        var result = await _repo.AddAsync(entity, cancellationToken);

        return result == 1 ? OperationResult<int>.OK(result) : OperationResult<int>.Failed("新增职位失败");
    }

    public async Task<OperationResult<int>> AddRangeAsync(Operation<IEnumerable<PositionCreateModel>> operation, CancellationToken cancellationToken = default)
    {
        var entities = _mapper.Map<IEnumerable<PositionCreateModel>, IEnumerable<Position>>(operation.Parameter);

        var result = await _repo.AddRangeAsync(entities, cancellationToken);

        return result > 0 ? OperationResult<int>.OK(result) : OperationResult<int>.Failed("批量新增职位失败");
    }

    public async Task<OperationResult<int>> AddIdentityRangeAsync(Operation<IEnumerable<PositionCreateModel>> operation, CancellationToken cancellationToken = default)
    {
        var entities = _mapper.Map<IEnumerable<PositionCreateModel>, IEnumerable<Position>>(operation.Parameter);

        var result = await _repo.AddIdentityRangeAsync(entities, cancellationToken);

        return result > 0 ? OperationResult<int>.OK(result) : OperationResult<int>.Failed("批量新增职位失败");
    }    
}